home *** CD-ROM | disk | FTP | other *** search
/ Language/OS - Multiplatform Resource Library / LANGUAGE OS.iso / cpp_libs / answrbok / 8_13.lha / 8_13 / 8_13b.c < prev    next >
C/C++ Source or Header  |  1993-08-08  |  1KB  |  51 lines

  1. * Copyright (c) 1990 by AT&T Bell Telephone Laboratories, Incorporated. */
  2. * The C++ Answer Book */
  3. * Tony Hansen */
  4. * All rights reserved. */
  5. / Check the scanset pattern for the character c.
  6. / Return complement for false and !complement for true.
  7. nt checkcharset(char *pattern, int complement, int c)
  8.  
  9.    char *svpat = pattern;
  10.    
  11.    // special case for pattern[0] == ']'
  12.    if ((*pattern == ']') || (*pattern == '-'))
  13. if (c == *pattern)
  14.     return !complement;
  15.  
  16.        else
  17.     pattern++;
  18.     
  19.    // run down the pattern
  20.    for ( ; *pattern && (*pattern != ']'); pattern++)
  21.        // either a range or trailing `-'
  22. if (*pattern == '-')
  23.     {
  24.     // special case for trailing `-'
  25.     if (pattern[1] == ']')
  26.     {
  27.     if (c == '-')
  28.         return !complement;
  29.     }
  30.     
  31.     // char range, a-z
  32.     else if ((c > pattern[-1]) &&
  33.              (c <= pattern[1]))
  34.     return !complement;
  35.  
  36.     // skip past the end value of the range
  37.     else
  38.     pattern++;
  39.     }
  40.  
  41. else if (*pattern == c)
  42.     return !complement;
  43.    
  44.    // The character is not in the set. pattern now
  45.    // points to the ']'. If c==-1, then return index
  46.    // of ']', else false.
  47.    if (c == -1)
  48.        return pattern - svpat;
  49.    return complement;
  50.  
  51.